<?php
class Pageley_Commerce_Command_CheckOut extends Pageley_Controller_Command_Abstract {

    protected function executeInternal() {
        $order = $this->getSession()->get(Pageley_Commerce_Session::ORDER);
        if($order === null || $order->get("orderItems") === null || $order->get("orderItems")->isEmpty()) {
            throw new Pageley_Domain_Exception(new Pageley_I18n_Message("error.commerce.checkOutEmptyOrder"));
        }
        $customer = $this->getSession()->get(Pageley_Commerce_Session::CUSTOMER);
        if($customer === null) {
            throw new Pageley_Domain_Exception(new Pageley_I18n_Message("error.commerce.notLoggedIn"));
        }
        if($order->get("billingAddress") === null) {
            $order->set("billingAddress", $customer->get("address"));
        }
        if($order->get("deliveryAddress") === null) {
            $order->set("deliveryAddress", $customer->get("address"));
        }
        if($order->get("billingName") === null) {
            if($customer->get("companyName") != null) {
                $order->set("billingName", $customer->get("companyName"));
            } else {
                $order->set("billingName", $customer->get("contactName"));
            }
        }
        if($order->get("billingEmailAddress") === null) {
            $order->set("billingEmailAddress", $customer->get("emailAddress"));
        }
        if($order->get("vatCode") === null && $customer->get("vatCode") !== null) {
            $order->set("vatCode", $customer->get("vatCode"));
        }
        if($order->get("deliveryName") === null) {
            if($customer->get("companyName") != null) {
                $order->set("deliveryName", $customer->get("companyName"));
            } else {
                $order->set("deliveryName", $customer->get("contactName"));
            }
        }
        if($order->get("sendInvoice") === null) {
            $order->set("sendInvoice", true);
        }
        $model = new Pageley_Util_ArrayMap();
        $billingAddress = $order->get("billingAddress");
        $deliveryAddress = $order->get("deliveryAddress");
        $model->put("billingName", $order->get("billingName"));
        $model->put("billingAddress.line1", $billingAddress->get("line1"));
        $model->put("billingAddress.line2", $billingAddress->get("line2"));
        $model->put("billingAddress.postalCode", $billingAddress->get("postalCode"));
        $model->put("billingAddress.city", $billingAddress->get("city"));
        $model->put("billingEmailAddress", $order->get("billingEmailAddress"));
        $model->put("vatCode", $order->get("vatCode"));
        $model->put("deliveryName", $order->get("deliveryName"));
        $model->put("deliveryAddress.line1", $deliveryAddress->get("line1"));
        $model->put("deliveryAddress.line2", $deliveryAddress->get("line2"));
        $model->put("deliveryAddress.postalCode", $deliveryAddress->get("postalCode"));
        $model->put("deliveryAddress.city", $deliveryAddress->get("city"));
        $model->put("paymentMethod", $order->get("paymentMethod"));
        $model->put("sendInvoice", $order->get("sendInvoice"));
        $model->put("comment", $order->get("comment"));
        return new Pageley_Controller_ModelAndView($model);
    }
}